package cn.antia.cep.right;

import java.util.*;

import cn.antia.cep.db.hibernate.bean.Resource;

public class RightTree {
	//子节点列表
	public List child;
	//当前节点
	public TreeNode node;
	
	/**
	 * 判断是否有子节点
	 *
     * @return bool*/
	public boolean hasChild(){
        return !(child == null || child.size() < 1);
    }
	/**
	 * 静态方法
	 * 判断传入的AntiaTree是否有子节点
	 *
     * @param tree 判断TREE是否有孩子
     * @return bool*/
	public static boolean hasChild(RightTree tree){
        return !(tree == null || tree.getNode() == null) && !(tree.getChild() == null || tree.getChild().size() < 1);
    }
	
	/**
	 * 普通的GET，SET方法
     * @return list childs*/
	public List getChild() {
		return child;
	}
	public void setChild(List child) {
		this.child = child;
	}
	public TreeNode getNode() {
		return node;
	}
	public void setNode(TreeNode node) {
		this.node = node;
	}
	
	
	/*
	public static void main(String[] args){
		System.out.println("start.........");
		RightTree antiaTreeRoot = new RightTree();
		Resource node = new Resource();
		//当前节点
		node.setCode("noderoot");
		node.setName("noderoot");
		node.setId(1);
		antiaTreeRoot.setNode(node);
		//子节点
		List childs = new LinkedList();
		//加一个节点
		RightTree antiaTree = new RightTree();
		node = new Resource();
		node.setCode("node1");
		node.setName("node1");
		node.setId(2);
		node.setParentId(1);
		antiaTree.setNode(node);
		
		List list = new ArrayList();
		node = new Resource();
		node.setCode("node1_1");
		node.setName("node1_1");
		node.setId(11);
		node.setParentId(1);
		RightTree tree = new RightTree();
		tree.setNode(node);
		list.add(tree);
		
		node = new Resource();
		node.setCode("node1_2");
		node.setName("node1_2");
		node.setId(12);
		node.setParentId(1);
		tree = new RightTree();
		tree.setNode(node);
		list.add(tree);
		
		antiaTree.setChild(list);
		
		childs.add(antiaTree);
		//再加一个节点
		antiaTree = new RightTree();
		node = new Resource();
		node.setCode("node2");
		node.setName("node2");
		node.setId(3);
		node.setParentId(1);
		antiaTree.setNode(node);
		childs.add(antiaTree);
		
		antiaTreeRoot.setChild(childs);
		recursiveXytree(antiaTreeRoot,0);
		
	}*/
	
	/**
	 * 递归取出树的内容
	 *
     * @param antiaTree 递归取数据*/
	public static void recursive(RightTree antiaTree){
		if(antiaTree.hasChild()){
			TreeNode node = antiaTree.getNode();
			System.out.println(node.getCode());
			List cs = antiaTree.getChild();
            for (Iterator it = cs.iterator(); it.hasNext();) {
                Object c = it.next();
                RightTree tree = (RightTree) c;
                recursive(tree);
            }
        }else{
        	TreeNode node = antiaTree.getNode();
			System.out.println(node.getCode());
        }
	}
	
	/**
	 * 递归输出XYTREE
	 * */
	public static void recursiveXytree(RightTree antiaTree,int i){
		if(antiaTree.hasChild()){
			TreeNode node = antiaTree.getNode();
			if(i == 0){
				String trees = "var resourceTree = new xyTree.DivTree('" + node.getName() + "')";
				System.out.println(trees);
			}else{
				String outs = "node" + i + " = new xyTree.Node('" + node.getName() + "');";
				System.out.println(outs);
				String outs1 = "node" + i + ".id=" + node.getId() + ";";
				System.out.println(outs1);
				if(i == 1){
					String outs2 = "resourceTree.add(node" + i + ");";
					System.out.println(outs2);
				}else{
					String outs2 = "node" + (i - 1) + ".add(node" + i + ");";
					System.out.println(outs2);
				}
			}
			//System.out.println(node.getCode());
			List cs = antiaTree.getChild();
            for (Iterator it = cs.iterator(); it.hasNext();) {
                Object c = it.next();
                RightTree tree = (RightTree) c;
                recursiveXytree(tree,++i);
            }
        }else{
        	TreeNode node = antiaTree.getNode();
			//System.out.println(node.getCode());
        	if(i == 0){
				String trees = "var resourceTree = new xyTree.DivTree('" + node.getName() + "')";
				System.out.println(trees);
			}else{
				String outs = "node" + i + " = new xyTree.Node('" + node.getName() + "');";
				System.out.println(outs);
				String outs1 = "node" + i + ".id=" + node.getId() + ";";
				System.out.println(outs1);
				if(i == 1){
					String outs2 = "resourceTree.add(node" + i + ");";
					System.out.println(outs2);
				}else{
					String outs2 = "node" + (i - 1) + ".add(node" + i + ");";
					System.out.println(outs2);
				}
			}
        }
	}
	
	
	public List toList(){
		List list = new LinkedList();
		if(node == null){
			return null;
		}
		list.add(node);
		list = recursiveToList(list,child);;
		return list;
	}
	/**
	 * 递归取出数据
	 * */
	private List recursiveToList(List returnList,List list){
		if(list == null){
			return list;
		}else{
			for(int i = 0;i<list.size();i++){
				RightTree tree = (RightTree)list.get(i);
				if(tree != null && tree.getNode() != null){
					returnList.add(tree.getNode());
				}
				recursiveToList(returnList,tree.getChild());
			}
		}
		return returnList;
	}
}
